package pers.cj.pdos.controller.menu;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.menu.entity.*;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.Menu3;
import pers.cj.pdos.service.IMenu1Service;
import pers.cj.pdos.utils.ClientInfo;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.menu.Menu1Vo;
import pers.cj.pdos.vo.menu.Menu2Vo;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class MenuController extends BaseController {
    private String baseUrl;

    @Autowired
    private IMenu1Service menu1Service;

    @ResponseBody
    @RequestMapping("/menu")
    public Menu menu(HttpServletRequest request){
        Company company = getCompany();
        baseUrl = ClientInfo.getBaseUrl(request);

        Menu menu = new Menu();

        menu.setHomeInfo(getHomeInfo());
        menu.setLogoInfo(getLogoInfo(company));


        List<MenuManager> managerList = new ArrayList<>();
        managerList.add(getMenuManager());
        menu.setMenuInfo(managerList);
        return menu;
    }

    private HomeInfo getHomeInfo(){
        HomeInfo homeInfo = new HomeInfo();
        //homeInfo.setIcon("fa fa-home");
        homeInfo.setTitle("主页");
        homeInfo.setHref(baseUrl + "/welcome.action");
        return homeInfo;
    }

    private LogoInfo getLogoInfo(Company company){
        LogoInfo logoInfo = new LogoInfo();
        logoInfo.setImage(baseUrl + "/img/company/" + company.getLogo());
        String name = company.getName();
        if (name.length() > 8) {
            name = name.substring(0,8) + "..";
        }
        logoInfo.setTitle(name);
        logoInfo.setHref(baseUrl + "/home.action");
        return logoInfo;
    }

    private MenuManager getMenuManager() {
        MenuManager manager = new MenuManager();
        manager.setIcon("fa fa-window-maximize");
        manager.setTitle("管理");
        manager.setTarget("_self");
        //管理下的1级菜单
        List<MenuOne> menuOnes = new ArrayList<>();
        List<Menu1Vo> menu1List = null;
        if (getLoginUsername().equals(getCompany().getAccount())) {
            //老板登录，则可以看到所有的菜单
            menu1List = menu1Service.listVo(null);
        } else {
            menu1List = menu1Service.listVo(getLoginUserId());
        }

        for (Menu1Vo menu1Vo : menu1List) {
            MenuOne one = new MenuOne();
            one.setTitle(menu1Vo.getName());
            one.setIcon(menu1Vo.getIcon());
            one.setTarget("_self");

            List<MenuTwo> menuTwos = new ArrayList<>();
            for (Menu2Vo menu2Vo : menu1Vo.getMenu2VoList()) {
                MenuTwo two = new MenuTwo();
                two.setTitle(menu2Vo.getName());
                two.setIcon(menu2Vo.getIcon());
                two.setTarget("_self");

                List<MenuThree> menuThrees = new ArrayList<>();
                for (Menu3 menu3 : menu2Vo.getMenu3List()) {
                    MenuThree three = new MenuThree();
                    three.setTitle(menu3.getName());
                    three.setIcon(menu3.getIcon());
                    three.setTarget(menu3.getTarget());
                    three.setHref(baseUrl + menu3.getUrl() + ".action");
                    menuThrees.add(three);
                }
                two.setChild(menuThrees);
                menuTwos.add(two);
            }
            one.setChild(menuTwos);
            menuOnes.add(one);
        }
        manager.setChild(menuOnes);
        return manager;
    }
}
